<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="企业" prop="workCorpId">
                            <el-select v-model="condition.search.workCorpId" placeholder="请选择企业"
                                       style="width:200px;">
                                <el-option
                                    v-for="item in corps"
                                    :key="item.id"
                                    :label="item.corpName"
                                    :value="item.id">
                                    {{ item.corpName }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="公司ID" prop="corpId">
                            <el-input v-model="condition.search.corpId" placeholder="公司ID"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="标签组ID" prop="tagGroupId">
                            <el-input v-model="condition.search.tagGroupId" placeholder="标签组ID"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="标签组名字" prop="name">
                            <el-input v-model="condition.search.name" placeholder="标签组名字"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="success" @click="sync">同步</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            size="small"
            :data="tableData"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="tagGroupId"
                label="标签组ID">
            </el-table-column>
            <el-table-column
                prop="name"
                label="标签组名字">
            </el-table-column>
            <el-table-column
                prop="tags"
                label="标签列表">
                <template v-slot="scope">
                    <el-tag
                        size="mini"
                        :key="tag.id"
                        v-for="tag in scope.row.tags"
                        closable
                        :disable-transitions="false"
                        @close="handleCloseTag(tag)">
                        {{ tag.name }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column
                prop="order"
                label="顺序">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间">
            </el-table-column>
            <el-table-column
                prop="syncTime"
                label="同步时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="80px">
                <template v-slot="scope">
                    <div>
                        <el-button @click="onRefresh(scope.row)" type="text" size="small">刷新</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px">
                <el-form-item label="公司ID">
                    <el-input v-model="addOrUpdateForm.corpId" placeholder="公司ID"></el-input>
                </el-form-item>
                <el-form-item label="标签组ID">
                    <el-input v-model="addOrUpdateForm.tagGroupId" placeholder="标签组ID"></el-input>
                </el-form-item>
                <el-form-item label="标签组名字">
                    <el-input v-model="addOrUpdateForm.name" placeholder="标签组名字"></el-input>
                </el-form-item>
                <el-form-item label="顺序">
                    <el-input v-model="addOrUpdateForm.order" placeholder="顺序"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="同步时间">
                    <el-date-picker
                        v-model="addOrUpdateForm.syncTime"
                        type="datetime"
                        placeholder="同步时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addWorkTagGroup,
    deleteWorkTagGroup,
    delWorkTag,
    getWorkCorpByPage,
    getWorkTagGroupByPage,
    refreshWorkTagGroup,
    syncWorkTagGroup,
    updateWorkTagGroup
} from '@/api/api'

export default {
    name: "WorkTagGroup",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},
            // 公司信息
            corps: [],
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        size: {
            type: String,
            default: 'small'
        },
        mode: {
            type: String,
            default: 'list'
        },
        workCorpId: String
    },
    watch: {
        workCorpId: function (val, oldVal) {
            this.condition.search.workCorpId = val;
        }
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        handleCloseTag: function (tag) {
            console.log(tag);
            delWorkTag(tag).then(data => {
                if (data.respCode == 1000) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                }
            })
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addWorkTagGroup(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateWorkTagGroup(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        sync: function () {
            if (!this.condition.search.workCorpId) {
                this.$message({
                    type: 'error',
                    message: '请先选择企业!'
                });
                return;
            }
            this.$confirm('请确认是否同步, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                syncWorkTagGroup({workCorpId: this.condition.search.workCorpId}).then(data => {
                    if (data.respCode == 1000) {
                        this.$message({
                            type: 'success',
                            message: '同步成功!'
                        });
                        this.getData();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        onRefresh: function (row) {
            this.$confirm('请确认是否刷新, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                refreshWorkTagGroup({id: row.id}).then(data => {
                    if (data.respCode == 1000) {
                        this.$message({
                            type: 'success',
                            message: '刷新成功!'
                        });
                        this.getData();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteWorkTagGroup({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取公司列表
        getCorps: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {}
            };
            getWorkCorpByPage(condition).then(data => {
                this.corps = data.body.rows;
            });
        },
        // 获取表格数据
        getData: function () {
            if (this.workCorpId) {
                this.condition.search.workCorpId = this.workCorpId;
            }
            getWorkTagGroupByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
            this.getCorps();
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
